package com.lyl.module.constant;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.map.MapUtil;
import cn.hutool.core.text.CharSequenceUtil;
import com.dtflys.forest.Forest;
import lombok.extern.slf4j.Slf4j;
import org.jsoup.Connection;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;

import java.util.Date;
import java.util.Map;

/**
 * @author Mr.Luo
 * @date 2022-07-21 10:20
 * @desc 阿里云公用变量
 */
@Slf4j
@SuppressWarnings("all")
public class ALIYUN_GLOBAL_VARIABLE {
    private static final String USER_AGENT = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/90.0.4430.212 Safari/537.36 Edg/90.0.818.66";
    public static final Integer DEFAULT_SIZE = 10480000;
    public static final int NEXT_REFRESH_TOKEN_TIME_INTERVAL = 24 * 60 * 60;
    //登录相关变量

    /**
     * 时间戳
     */
    public static String T;
    /**
     * CK
     */
    public static String CK;

    /**
     * ACCESS_TOKEN
     */
    public static  String ACCESS_TOKEN;

    /**
     * client_id
     */
    public static String CLIENT_ID = getClientId();

    /**
     * cookie
     */
    public static String COOKIE = getCookie();

    public static String DRIVE_ID;

    public static String REFRESH_TOKEN = "a17b480eba164eb6afacbaec322d0f0c";

    public static Date NEXT_REFRESH_TOKEN_TIME;

    public static String TOKEN_TYPE;

    public static String AUTHORIZATION = getAuthorization();

    public static String getAuthorization() {
        return TOKEN_TYPE + " " + ACCESS_TOKEN;
    }

    static {
        Forest.config().setVariableValue("refresh_token",REFRESH_TOKEN);
        log.info("refresh_token = {}",REFRESH_TOKEN);
    }

    /**
     * 获取 client_id
     * @return clientId
     */
    public static String getClientId(){
        Connection connect = Jsoup.connect("https://aliyundrive.com/sign/in");
        Connection data = connect.header("User-Agent",USER_AGENT).header("referer","https://aliyundrive.com/");
        try {
            Document doc = data.get();
            return CharSequenceUtil.subBetween(doc.html(), "client_id: '", "'");
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "";
    }

    public static void setNextRefreshTokenTime(){
        NEXT_REFRESH_TOKEN_TIME = DateUtil.offsetSecond(DateUtil.date(),NEXT_REFRESH_TOKEN_TIME_INTERVAL);
    }

    public static void setDriveId(String driveId){
        DRIVE_ID = driveId;
    }

    /**
     * 获取阿里云 Cookie
     * @return cookie
     */
    public static String getCookie()  {
        String cookie = "";
        try {
            Map<String, String> map2 = initCookie(AliYunUrlConstant.AUTHENTICATION_URL);
            Map<String, String> map3  = initCookie("https://passport.aliyundrive.com/mini_login.htm?lang=zh_cn&appName=aliyun_drive");
            cookie = mapToString(map2)+";";
            cookie+= mapToString(map3);
            Forest.config().setVariableValue("cookie",cookie);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return cookie;
    }

    /**
     * map转 String
     * @param map
     * @return
     */
    private static String mapToString(Map<String, String> map) {
        return MapUtil.join(map, "; ", "=", null);
    }

    /**
     * 初始化阿里云 Cookie
     * @param url
     * @return
     * @throws Exception
     */
    private static Map<String, String> initCookie(String url) throws Exception {
        Connection connect = Jsoup.connect(url);
        Connection data = connect.header("User-Agent",USER_AGENT)
                .header("referer","https://aliyundrive.com/");
        Document doc = data.get();
        data.response().headers();
        return data.response().cookies();
    }
}
